package pl.veneo.wigprofit.view {
	import pl.veneo.wigprofit.model.Configuration;
	import pl.veneo.wigprofit.ctrl.events.ChartResizeEvent;
	import pl.veneo.wigprofit.model.PeriodData;
	import pl.veneo.wigprofit.view.elements.BackButton;
	import pl.veneo.wigprofit.view.elements.ChartArrow;
	import pl.veneo.wigprofit.view.elements.ChartLines;
	import pl.veneo.wigprofit.view.elements.ChartScales;
	import pl.veneo.wigprofit.view.elements.LegendElement;
	import pl.veneo.wigprofit.view.elements.SliderBar;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class ChartView extends EventDispatcher {
		
		private var container : Sprite;
		private var _currentPeriod : PeriodData;
		private var _chartBackground : Sprite;
		private var _chartLines : ChartLines;
		private var _chartScales : ChartScales;
		private var _chartArrow : ChartArrow;
		private var _slider : SliderBar;
		private var _legend : LegendElement;
		private var _backbutton : BackButton;

		public function ChartView(container : Sprite) {
			super();

			this.container = container;

			_chartBackground = container.addChild(new Sprite()) as Sprite;
			_chartBackground.graphics.beginFill(Configuration.getInstance().bgColor, 1);
			_chartBackground.graphics.drawRect(0, 0, maxWidth - 75, maxHeigth - 55);
			_chartBackground.graphics.endFill();
			_chartBackground.x = (590 - _chartBackground.width) / 2;
			_chartBackground.y = - 10 + (400 - _chartBackground.height) / 2;
			// _chartBackground.visible = false;

			_chartScales = container.addChild(new ChartScales(_chartBackground.width, _chartBackground.height)) as ChartScales;
			_chartScales.x = _chartBackground.x;
			_chartScales.y = _chartBackground.y;

			_chartLines = container.addChild(new ChartLines(_chartBackground.width, _chartBackground.height)) as ChartLines;
			_chartLines.x = _chartBackground.x;
			_chartLines.y = _chartBackground.y;
			_chartLines.mouseEnabled = false;
			_chartLines.mouseChildren = false;

			_chartArrow = container.addChild(new ChartArrow(_chartBackground.width, _chartBackground.height)) as ChartArrow;
			_chartArrow.x = _chartBackground.x;
			_chartArrow.y = _chartBackground.y;
			_chartArrow.addEventListener(ChartResizeEvent.ON_CHART_RESIZE, onZoomChart);

			_legend = container.addChild(new LegendElement()) as LegendElement;
			_legend.x = _chartBackground.x + 7;
			_legend.y = _chartBackground.y + 7;
			_legend.addEventListener(LegendElement.TOGGLE_CHART, onChartToggle);

			_slider = container.addChild(new SliderBar(_chartBackground.width, _chartBackground.height)) as SliderBar;
			_slider.x = _chartBackground.x;
			_slider.y = _chartBackground.y + _chartBackground.height + 20;
			_slider.addEventListener(ChartResizeEvent.ON_CHART_RESIZE, onSlideChart);
			_slider.addEventListener(ChartResizeEvent.ON_CHART_SLIDE, onSlideChart);

			_backbutton = container.addChild(new BackButton()) as BackButton;
			_backbutton.x = _chartBackground.x + _chartBackground.width - _backbutton.width - 7;
			_backbutton.y = _chartBackground.y + _chartBackground.height - _backbutton.height - 7;
			_backbutton.addEventListener(MouseEvent.CLICK, onResetZoom);
			_backbutton.visible = false;
			
			CursorManager.getInstance(container);
		}
		
		public function showButton() : void {
			
			_backbutton.visible = true;
		}

		private function onResetZoom(event : MouseEvent) : void {
			_backbutton.visible = false;
			dispatchEvent(new ChartResizeEvent(ChartResizeEvent.ON_RESET_ZOOM));
		}

		private function onChartToggle(event : Event) : void {
			_chartLines.showChart1(_legend.toggle1);
			_chartLines.showChart2(_legend.toggle2);
		}

		private function onSlideChart(event : ChartResizeEvent) : void {
			
			_backbutton.visible = true;
			dispatchEvent(new ChartResizeEvent(ChartResizeEvent.ON_CHART_SLIDE, event.start, event.end, event.force));
		}

		private function onZoomChart(event : ChartResizeEvent) : void {
		
			_backbutton.visible = true;
			dispatchEvent(new ChartResizeEvent(ChartResizeEvent.ON_CHART_RESIZE, event.start, event.end, event.force));
		}

		public function buildZoomForPeriod(period : PeriodData) : void {
			
			_slider.showPeriod(period);
			buildSlideForPeriod(period);
		}

		public function buildSlideForPeriod(period : PeriodData) : void {
			
			_chartLines.draw(period);
			_chartScales.draw(period);
			_chartArrow.draw(period);
		}

		public function buildChartForPeriod(period : PeriodData) : void {
			_currentPeriod = period;
			_chartLines.draw(period);
			_chartScales.draw(period);
			_chartArrow.draw(period);
			_slider.init(period);
		}

		private function get maxWidth() : Number {
			return container.stage.stageWidth;
		}

		private function get maxHeigth() : Number {
			return container.stage.stageHeight;
		}
	}
}
